************************************************************
*****     		Create relevant samples			       *****
************************************************************

/* CONTENTS:
- Set Paths
- Create program
- Set Delta and age upper bound

- Hospitalizations
1. Overall sample
2. Robustness Check
3. Heterogeneity Analysis

- Mortality 
1. Overall sample
2. Accident sample 
3. Robustness Check
*/


********************************************
* Set folders and paths
********************************************
cd $processed_data
cap mkdir !temp
cd !temp

********************************************
* Program to create precise subsets of treatment vars
********************************************
cap program drop IndexVar
program IndexVar
	macro drop var*
	args varlist
	des `varlist', varlist
	local variables = r(varlist)
	local N : word count `variables'
	local group = 50
	global index= ceil(`N'/`group') //split variables into groups of 50, take ceiling
	local endpoint = `group'
	global varS `: word 1 of `variables'' //index first variable
	forvalues i = 1/$index {
		if `endpoint' >= `N' { //index last variable
			global varL `: word `N' of `variables''	
		}
		else { //create start and end points to precisely define groups
			local startpoint = `endpoint'-1
			global var`i'_s `: word `startpoint' of `variables''
			global var`i'_e `: word `endpoint' of `variables''
		}
		local endpoint = `group'*(`i'+1)
	}
end 

exit 

********************************************
* Set Delta and age upper bound
********************************************
*run for each delta
global delta = 4
if $delta == 2 {
	global age_UB=20
}
else if $delta == 3 {
	global age_UB=21
}
else if $delta == 4 {
	global age_UB=22
}
else if $delta == 5 {
	global age_UB=23
}

*********************************************************
****************** Hospitalizations *********************
*********************************************************

*********************************************
*** 1. Overall sample
*********************************************
{
timer clear 1
timer on 1
forvalues childbyear= 1990(1)2015 {
	
	use "$processed_data\matched.dta", clear 
	
	keep if child_b_year == `childbyear'
	
	g yearinphospital = year(first_inphospital)
	g ageinphospital = yearinphospital - child_b_year
	keep if ageinphospital >= 7 & ageinphospital <= $age_UB
	keep if mother_b_year >= 1942 & mother_b_year <=1998
	keep if father_b_year >= 1925 & father_b_year <=1995
	
	egen bdyid = group(mother_b_year father_b_year)
	compress 
	
	// Create binary variables representing treatment groups 
	/// for each mother birthyear, father birthyear, and child's age at health shock 
	levelsof bdyid, local(bdy_levels)
	foreach bdy of local bdy_levels {
		qui levelsof ageinphospital, local(age_levels)
		foreach age of local age_levels {
			gen byte treat_`age'_`bdy' = . 
			replace treat_`age'_`bdy' = 1 if ///
			ageinphospital== `age' & bdyid == `bdy' 
			replace treat_`age'_`bdy' = 0 if ///
			ageinphospital==(`age'+ $delta) & bdyid == `bdy'
			qui su treat_`age'_`bdy', de 
			if r(min) == r(max) {
				drop treat_`age'_`bdy'
			}
		}
	}
	save "temp_MB.dta", replace 
	
	// Create a set of global variables that index the above binary variables
	use "temp_MB.dta", clear 

	capture IndexVar treat*
	if _rc == 111 { //variable treat* not found (occurs for years 2008-2015 in delta4);
		save "tf`childbyear'_all_$delta.dta", replace
		continue 
	}
	
	// Reshape horizontally chunked data so it is long, uniquely identified
	/// by mother's ID and treatment group
	if $index <= 2 { //too few variables to run loop (occurs in 2007)
	    reshape long treat_, i(shnro_mother) j(group) string
		rename treat_ treat
		duplicates report shnro_mother
		drop if missing(treat)
		rename ageinphospital ageinphospitalnew

		split group, p("_")
		destring group1, replace
		rename group1 ageinptreat

		save "tf`childbyear'_all_$delta.dta", replace 
	}
	
	else {
		local index = $index - 1
		forvalues i = 1/`index' {
			use "temp_MB.dta", clear 	
			if `i' == 1 { //startpoint is special case
				keep shnro_mother-bdyid ${varS}-${var2_s} 
			}
			else if `i' == `index' { //endpoint is a special case 
				local startindex = `i'
				keep shnro_mother-bdyid ${var`startindex'_e}-${varL}
			}
			else {
				local startindex = `i'
				local endindex = `startindex' + 1
				keep shnro_mother-bdyid ${var`startindex'_e}-${var`endindex'_s}
			}
			reshape long treat_, i(shnro_mother) j(group) string
			rename treat_ treat
			duplicates report shnro_mother
			drop if missing(treat)
			
			save "tf`childbyear'_`i'.dta", replace
		}

		
		// Append and remove datasets for child birth year 
		local index = $index - 1
		use "tf`childbyear'_1.dta", clear
		forvalues i=2(1)`index' {
			append using "tf`childbyear'_`i'.dta"
			rm "tf`childbyear'_`i'.dta"
		}

		rename ageinphospital ageinphospitalnew

		split group, p("_")
		destring group1, replace
		rename group1 ageinptreat
		drop group group2 
		
		save "tf`childbyear'_all_$delta.dta", replace 

		rm "tf`childbyear'_1.dta"
	}
}
timer off 1
timer list 
// 4802 seconds (1 hour and 20 min) for delta == 4
// 4802 seconds (1 hour and 20 min) for delta == 3
// 4757 seconds (1 hour and 20 min) for delta == 2

local index = $index - 1
use "tf1990_all_$delta.dta", clear
forvalues i=1991(1)2015 {
	append using "tf`i'_all_$delta.dta"
}

* Drop missing treat values 
** Occurs when no possible treatment-control pairings exist for a given child birth year.
drop if missing(treat) 

compress
save "$processed_data\tf_all_${delta}.dta", replace 
}

*********************************************
*** 2. Robustness Check
**** Mutual shock
*********************************************
{

foreach time in "month" "week" {
	forvalues childbyear= 1990(1)2015 {
		
		use "$processed_data\matched_mutual_`time'.dta", clear 
		
		keep if child_b_year == `childbyear'
		
		g yearinphospital = year(first_inphospital)
		g ageinphospital = yearinphospital - child_b_year
		keep if ageinphospital>=7 & ageinphospital <= $age_UB
		keep if mother_b_year >= 1942 & mother_b_year <=1998
		keep if father_b_year >= 1925 & father_b_year <=1995
		
		egen bdyid = group(mother_b_year father_b_year)
		compress 
		
		// Create binary variables representing treatment groups 
		/// for each mother birthyear, father birthyear, and child's age at health shock 
		levelsof bdyid, local(bdy_levels)
		foreach bdy of local bdy_levels {
			qui levelsof ageinphospital, local(age_levels)
			foreach age of local age_levels {
				gen byte treat_`age'_`bdy' = . 
				replace treat_`age'_`bdy' = 1 if ///
				ageinphospital== `age' & bdyid == `bdy' 
				replace treat_`age'_`bdy' = 0 if ///
				ageinphospital==(`age'+ $delta) & bdyid == `bdy'
				qui su treat_`age'_`bdy', de 
				if r(min) == r(max) {
					drop treat_`age'_`bdy'
				}
			}
		}
		save "temp_MB.dta", replace 
		
		// Create a set of global variables that index the above binary variables
		use "temp_MB.dta", clear 

		capture IndexVar treat*
		if _rc == 111 { //variable treat* not found (occurs for years 2008-2015 in delta4);
			save "tf`childbyear'_all_$delta.dta", replace
			continue 
		}
		
		// Reshape horizontally chunked data so it is long, uniquely identified
		/// by mother's ID and treatment group
		if $index <= 2 { //too few variables to run loop (occurs in 2007)
			reshape long treat_, i(shnro_mother) j(group) string
			rename treat_ treat
			duplicates report shnro_mother
			drop if missing(treat)
			rename ageinphospital ageinphospitalnew

			split group, p("_")
			destring group1, replace
			rename group1 ageinptreat

			save "tf`childbyear'_all_$delta.dta", replace 
		}
		
		else {
			local index = $index - 1
			forvalues i = 1/`index' {
				use "temp_MB.dta", clear 	
				if `i' == 1 { //startpoint is special case
					keep shnro_mother-bdyid ${varS}-${var2_s} 
				}
				else if `i' == `index' { //endpoint is a special case 
					local startindex = `i'
					keep shnro_mother-bdyid ${var`startindex'_e}-${varL}
				}
				else {
					local startindex = `i'
					local endindex = `startindex' + 1
					keep shnro_mother-bdyid ${var`startindex'_e}-${var`endindex'_s}
				}
				reshape long treat_, i(shnro_mother) j(group) string
				rename treat_ treat
				duplicates report shnro_mother
				drop if missing(treat)
				
				save "tf`childbyear'_`i'.dta", replace
			}

			
			// Append and remove datasets for child birth year 
			local index = $index - 1
			use "tf`childbyear'_1.dta", clear
			forvalues i=2(1)`index' {
				append using "tf`childbyear'_`i'.dta"
				rm "tf`childbyear'_`i'.dta"
			}

			rename ageinphospital ageinphospitalnew

			split group, p("_")
			destring group1, replace
			rename group1 ageinptreat
			drop group group2 
			
			save "tf`childbyear'_all_$delta.dta", replace 

			rm "tf`childbyear'_1.dta"
		}
	}
	timer off 1
	timer list 


	local index = $index - 1
	use "tf1990_all_$delta.dta", clear
	forvalues i=1991(1)2015 {
		append using "tf`i'_all_$delta.dta"
	}

	* Drop missing treat values 
	** Occurs when no possible treatment-control pairings exist for a given child birth year
	drop if missing(treat) 

	compress
	save "$processed_data\tf_all_`time'_${delta}.dta", replace 
}
}

*********************************************
*** 3. Heterogeneity Analysis
**** Burden of care and diagnosis 
*********************************************

{
	
* Burden of care
foreach intensity in "h" "l" {
	forvalues childbyear= 1990(1)2015 {

		use "$processed_data\matched_`intensity'_mean.dta", clear 
		
		keep if child_b_year == `childbyear'
		
		cap drop yearinphospital 
		g yearinphospital = year(first_inphospital)
		g ageinphospital = yearinphospital - child_b_year
		keep if ageinphospital>=7 & ageinphospital <= $age_UB
		keep if mother_b_year >= 1942 & mother_b_year <=1998
		keep if father_b_year >= 1925 & father_b_year <=1995
		
		egen bdyid = group(mother_b_year father_b_year)
		compress 
		
		// Create binary variables representing treatment groups 
		/// for each mother birthyear, father birthyear, and child's age at health shock 
		levelsof bdyid, local(bdy_levels)
		foreach bdy of local bdy_levels {
			qui levelsof ageinphospital, local(age_levels)
			foreach age of local age_levels {
				gen byte treat_`age'_`bdy' = . 
				replace treat_`age'_`bdy' = 1 if ///
				ageinphospital== `age' & bdyid == `bdy' 
				replace treat_`age'_`bdy' = 0 if ///
				ageinphospital==(`age'+ $delta) & bdyid == `bdy'
				qui su treat_`age'_`bdy', de 
				if r(min) == r(max) {
					drop treat_`age'_`bdy'
				}
			}
		}
		save "temp_MB.dta", replace 
		
		// Create a set of global variables that index the above binary variables
		use "temp_MB.dta", clear 

		capture IndexVar treat*
		if _rc == 111 { //variable treat* not found (occurs for years 2008-2015 in delta4);
			save "tf`childbyear'_all_$delta.dta", replace
			continue 
		}
		
		// Reshape horizontally chunked data so it is long, uniquely identified
		/// by mother's ID and treatment group
		if $index <= 2 { //too few variables to run loop (occurs in 2007)
			reshape long treat_, i(shnro_mother) j(group) string
			rename treat_ treat
			duplicates report shnro_mother
			drop if missing(treat)
			rename ageinphospital ageinphospitalnew

			split group, p("_")
			destring group1, replace
			rename group1 ageinptreat

			save "tf`childbyear'_all_$delta.dta", replace 
		}
		
		else {
			local index = $index - 1
			forvalues i = 1/`index' {
				use "temp_MB.dta", clear 	
				if `i' == 1 { //startpoint is special case
					keep shnro_mother-bdyid ${varS}-${var2_s} 
				}
				else if `i' == `index' { //endpoint is a special case 
					local startindex = `i'
					keep shnro_mother-bdyid ${var`startindex'_e}-${varL}
				}
				else {
					local startindex = `i'
					local endindex = `startindex' + 1
					keep shnro_mother-bdyid ${var`startindex'_e}-${var`endindex'_s}
				}
				reshape long treat_, i(shnro_mother) j(group) string
				rename treat_ treat
				duplicates report shnro_mother
				drop if missing(treat)
				
				save "tf`childbyear'_`i'.dta", replace
			}

			
			// Append and remove datasets for child birth year 
			local index = $index - 1
			use "tf`childbyear'_1.dta", clear
			forvalues i=2(1)`index' {
				append using "tf`childbyear'_`i'.dta"
				rm "tf`childbyear'_`i'.dta"
			}

			rename ageinphospital ageinphospitalnew

			split group, p("_")
			destring group1, replace
			rename group1 ageinptreat
			drop group group2 
			
			save "tf`childbyear'_all_$delta.dta", replace 

			rm "tf`childbyear'_1.dta"
		}
	}
	timer off 1
	timer list 


	local index = $index - 1
	use "tf1990_all_$delta.dta", clear
	forvalues i=1991(1)2015 {
		append using "tf`i'_all_$delta.dta"
	}

	* Drop missing treat values 
	** Occurs when no possible treatment-control pairings exist for a given child birth year
	drop if missing(treat) 

	compress
	save "$processed_data\tf_all_`intensity'_${delta}.dta", replace 
}



* Diagnosis
foreach subgroup in "cancer" "skin" {

	use "$processed_data\matched_hg.dta", clear 
	
	keep if `subgroup' == 1
	
	g yearinphospital = year(first_inphospital)
	g ageinphospital = yearinphospital - child_b_year
	keep if ageinphospital>=7 & ageinphospital <= $age_UB
	keep if child_b_year >=1990 & child_b_year<=2015
	
	* Ensure mothers and fathers have the same amount of observations
	keep if !missing(mother_b_year) & !missing(father_b_year)
	
	*egen bdyid = group(child_b_year mother_b_year father_b_year)
	
	// Create binary variables representing treatment groups 
	/// for each mother birthyear, father birthyear, and child's age at health shock 
	levelsof child_b_year, local(bdy_levels)
	foreach bdy of local bdy_levels {
		qui levelsof ageinphospital, local(age_levels)
		foreach age of local age_levels {
			gen byte treat_`age'_`bdy' = . 
			replace treat_`age'_`bdy' = 1 if ///
			ageinphospital== `age' & child_b_year == `bdy' 
			replace treat_`age'_`bdy' = 0 if ///
			ageinphospital==(`age'+ $delta) & child_b_year == `bdy'
			qui su treat_`age'_`bdy', de 
			if r(min) == r(max) {
				drop treat_`age'_`bdy'
			}
		}
	}

	reshape long treat_, i(shnro_mother) j(group) string
	rename treat_ treat
	drop if missing(treat)
	duplicates report shnro_mother
	rename ageinphospital ageinphospitalnew

	split group, p("_")
	destring group1, replace
	rename group1 ageinptreat
	tab ageinptreat,m 

	tab treat,m 

	* Drop missing treat values 
	** Occurs when no possible treatment-control pairings exist for a given child birth year
	drop if missing(treat) 

	compress
	save "$processed_data\tf_all_`subgroup'_${delta}.dta", replace 
}

}
********************************************
* Location
********************************************

{
use "$processed_data\matched.dta", clear 
merge 1:1 shnro_mother using ///
"$processed_data\location.dta", keepusing(mother_same_region)
drop if _merge == 2
drop _merge
keep if mother_same_region == 0
save "$processed_data\matched_location.dta", replace 

timer clear 1
timer on 1
forvalues childbyear= 1990(1)2015 {
	
	use "$processed_data\matched_location.dta", clear 
	
	keep if child_b_year == `childbyear'
	
	g yearinphospital = year(first_inphospital)
	g ageinphospital = yearinphospital - child_b_year
	keep if ageinphospital>=6 & ageinphospital <= $age_UB
	keep if mother_b_year >= 1942 & mother_b_year <=1998
	keep if father_b_year >= 1925 & father_b_year <=1995
	
	egen bdyid = group(mother_b_year father_b_year)
	compress 
	
	// Create binary variables representing treatment groups 
	/// for each mother birthyear, father birthyear, and child's age at health shock 
	levelsof bdyid, local(bdy_levels)
	foreach bdy of local bdy_levels {
		qui levelsof ageinphospital, local(age_levels)
		foreach age of local age_levels {
			gen byte treat_`age'_`bdy' = . 
			replace treat_`age'_`bdy' = 1 if ///
			ageinphospital== `age' & bdyid == `bdy' 
			replace treat_`age'_`bdy' = 0 if ///
			ageinphospital==(`age'+ $delta) & bdyid == `bdy'
			qui su treat_`age'_`bdy', de 
			if r(min) == r(max) {
				drop treat_`age'_`bdy'
			}
		}
	}
	save "temp_MB.dta", replace 
	
	// Create a set of global variables that index the above binary variables
	use "temp_MB.dta", clear 

	capture IndexVar treat*
	if _rc == 111 { //variable treat* not found (occurs for years 2008-2015 in delta4);
		save "tf`childbyear'_all_$delta.dta", replace
		continue 
	}
	
	// Reshape horizontally chunked data so it is long, uniquely identified
	/// by mother's ID and treatment group
	if $index <= 2 { //too few variables to run loop (occurs in 2007)
	    reshape long treat_, i(shnro_mother) j(group) string
		rename treat_ treat
		duplicates report shnro_mother
		drop if missing(treat)
		rename ageinphospital ageinphospitalnew

		split group, p("_")
		destring group1, replace
		rename group1 ageinptreat

		save "tf`childbyear'_all_$delta.dta", replace 
	}
	
	else {
		local index = $index - 1
		forvalues i = 1/`index' {
			use "temp_MB.dta", clear 	
			if `i' == 1 { //startpoint is special case
				keep shnro_mother-bdyid ${varS}-${var2_s} 
			}
			else if `i' == `index' { //endpoint is a special case 
				local startindex = `i'
				keep shnro_mother-bdyid ${var`startindex'_e}-${varL}
			}
			else {
				local startindex = `i'
				local endindex = `startindex' + 1
				keep shnro_mother-bdyid ${var`startindex'_e}-${var`endindex'_s}
			}
			reshape long treat_, i(shnro_mother) j(group) string
			rename treat_ treat
			duplicates report shnro_mother
			drop if missing(treat)
			
			save "tf`childbyear'_`i'.dta", replace
		}

		
		// Append and remove datasets for child birth year 
		local index = $index - 1
		use "tf`childbyear'_1.dta", clear
		forvalues i=2(1)`index' {
			append using "tf`childbyear'_`i'.dta"
			rm "tf`childbyear'_`i'.dta"
		}

		rename ageinphospital ageinphospitalnew

		split group, p("_")
		destring group1, replace
		rename group1 ageinptreat
		drop group group2 
		
		save "tf`childbyear'_all_$delta.dta", replace 

		rm "tf`childbyear'_1.dta"
	}
}
timer off 1
timer list 
// 4802 seconds (1 hour and 20 min) for delta == 4
// 4802 seconds (1 hour and 20 min) for delta == 3
// 4757 seconds (1 hour and 20 min) for delta == 2

local index = $index - 1
use "tf1990_all_$delta.dta", clear
forvalues i=1991(1)2015 {
	append using "tf`i'_all_$delta.dta"
}

* Drop missing treat values 
** Occurs when no possible treatment-control pairings exist for a given child birth year
drop if missing(treat) 

compress
save "$processed_data\tf_all_region0_${delta}.dta", replace 

rm "$processed_data\matched_location.dta"
}


*********************************************************
********************** Mortality ************************
*********************************************************

********************************************
* Set Delta and age upper bound
********************************************
global delta = 4
if $delta == 2 {
	global age_UB=20
}
else if $delta == 3 {
	global age_UB=21
}
else if $delta == 4 {
	global age_UB=22
}
else if $delta == 5 {
	global age_UB=23
}
*********************************************
*** 1. Overall sample
*********************************************

{
use "$processed_data\matched_mortality.dta", clear 

g yearmortality = year(first_mortality)
g agemortality = yearmortality - child_b_year
keep if agemortality>=7 & agemortality <= $age_UB
keep if child_b_year >=1990 & child_b_year<=2015
* Ensure mothers and fathers have the same amount of observations
keep if !missing(mother_b_year) & !missing(father_b_year)

// Create binary variables representing treatment groups 
/// for each mother birthyear, father birthyear, and child's age at health shock 
levelsof child_b_year, local(bdy_levels)
foreach bdy of local bdy_levels {
	qui levelsof agemortality, local(age_levels)
	foreach age of local age_levels {
		gen byte treat_`age'_`bdy' = . 
		replace treat_`age'_`bdy' = 1 if ///
		agemortality== `age' & child_b_year == `bdy' 
		replace treat_`age'_`bdy' = 0 if ///
		agemortality==(`age'+ $delta) & child_b_year == `bdy'
		qui su treat_`age'_`bdy', de 
		if r(min) == r(max) {
			drop treat_`age'_`bdy'
		}
	}
}

reshape long treat_, i(shnro_mother) j(group) string
rename treat_ treat
drop if missing(treat)
duplicates report shnro_mother
rename agemortality agemortalitynew

split group, p("_")
destring group1, replace
rename group1 ageinptreat
tab ageinptreat,m 

tab treat,m 

* Drop missing treat values 
** Occurs when no possible treatment-control pairings exist for a given child birth year
drop if missing(treat) 

compress
save "$processed_data\tf_all_mortality_${delta}.dta", replace 
}

*********************************************
*** 2. Accident sample
*********************************************
{
	
use "$processed_data\matched_mortality.dta", clear 

* Merge in variable to create accident indicator  
merge 1:1 shnro_mother using "$processed_data\date_mortality.dta", keepusing(tpksaika)
drop if _merge == 2
drop _merge
destring tpksaika, g (cause)
g accident = 1 if cause >= 42 & cause <= 53
keep if accident == 1
drop tpksaika cause

g yearmortality = year(first_mortality)
g agemortality = yearmortality - child_b_year
keep if agemortality>=7 & agemortality <= $age_UB
keep if child_b_year >=1990 & child_b_year<=2015
* Ensure mothers and fathers have the same amount of observations
keep if !missing(mother_b_year) & !missing(father_b_year)

// Create binary variables representing treatment groups 
/// for each mother birthyear, father birthyear, and child's age at health shock 
levelsof child_b_year, local(bdy_levels)
foreach bdy of local bdy_levels {
	qui levelsof agemortality, local(age_levels)
	foreach age of local age_levels {
		gen byte treat_`age'_`bdy' = . 
		replace treat_`age'_`bdy' = 1 if ///
		agemortality== `age' & child_b_year == `bdy' 
		replace treat_`age'_`bdy' = 0 if ///
		agemortality==(`age'+ $delta) & child_b_year == `bdy'
		qui su treat_`age'_`bdy', de 
		if r(min) == r(max) {
			drop treat_`age'_`bdy'
		}
	}
}

reshape long treat_, i(shnro_mother) j(group) string
rename treat_ treat
drop if missing(treat)
duplicates report shnro_mother
rename agemortality agemortalitynew

split group, p("_")
destring group1, replace
rename group1 ageinptreat
tab ageinptreat,m 

tab treat,m 

* Drop missing treat values 
** Occurs when no possible treatment-control pairings exist for a given child birth year
drop if missing(treat) 

compress
save "$processed_data\tf_all_mortality_accident_${delta}.dta", replace 

}

*********************************************
*** 3. Robustness check 
**** Mutual shock (accident)
*********************************************
{

foreach time in "month" "week" {
	use "$processed_data\matched_mortality_mutual_`time'.dta", clear 

	* Merge in variable to create accident indicator  
	merge 1:1 shnro_mother using "$processed_data\date_mortality.dta", ///
	keepusing(tpksaika)
	drop if _merge == 2
	drop _merge
	destring tpksaika, g (cause)
	g accident = 1 if cause >= 42 & cause <= 53
	keep if accident == 1
	drop tpksaika cause

	g yearmortality = year(first_mortality)
	g agemortality = yearmortality - child_b_year
	keep if agemortality>=7 & agemortality <= $age_UB
	keep if child_b_year >=1990 & child_b_year<=2015

	* Ensure mothers and fathers have the same amount of observations
	keep if !missing(mother_b_year) & !missing(father_b_year)

	// Create binary variables representing treatment groups 
	/// for each mother birthyear, father birthyear, and child's age at health shock 
	levelsof child_b_year, local(bdy_levels)
	foreach bdy of local bdy_levels {
		qui levelsof agemortality, local(age_levels)
		foreach age of local age_levels {
			gen byte treat_`age'_`bdy' = . 
			replace treat_`age'_`bdy' = 1 if ///
			agemortality== `age' & child_b_year == `bdy' 
			replace treat_`age'_`bdy' = 0 if ///
			agemortality==(`age'+ $delta) & child_b_year == `bdy'
			qui su treat_`age'_`bdy', de 
			if r(min) == r(max) {
				drop treat_`age'_`bdy'
			}
		}
	}

	reshape long treat_, i(shnro_mother) j(group) string
	rename treat_ treat
	drop if missing(treat)
	duplicates report shnro_mother
	rename agemortality agemortalitynew

	split group, p("_")
	destring group1, replace
	rename group1 ageinptreat
	tab ageinptreat,m 

	tab treat,m 

	* Drop missing treat values 
	** Occurs when no possible treatment-control pairings exist for a given child birth year
	drop if missing(treat) 

	compress
	save "$processed_data\tf_all_mortality_acc_`time'_${delta}.dta", replace 

}

}
